package com.zpyr.mvc.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import org.mybatis.spring.SqlSessionTemplate;

import com.zpyr.common.SessionManager;
import com.zpyr.common.StringUtil;
import com.zpyr.mvc.interfaces.CarcMapper;
import com.zpyr.mvc.interfaces.HomeMapper;
import com.zpyr.mvc.vo.Home_memo;

@Controller
public class MemoCont {
	
	@Autowired
	private SqlSessionTemplate sqlSession;
	private final String url ="/home/memo"; 
	/**
	 * 메모 메인 뷰
	 */
	@RequestMapping(value=url + ".zpr" , params={"!nouse"})
	public ModelAndView memo_main(HttpServletRequest req , String cur , String num) {// RequestParam <- 파라미터가 없으면 400에러를 발생함. 지나친 400에러 발생은 문제의 소지가 있기에 지켜보기로 함.
		HomeMapper hm = sqlSession.getMapper(HomeMapper.class);
		
		SessionManager sess = SessionManager.getInstance(req);
		
		String user_id = sess.getLoginEmail();
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("memo", "");
		map.put("title", "");
		num = StringUtil.nvl(num, "5");
		map.put("num", num);
		cur = StringUtil.nvl(cur, "0");
		map.put("cur", cur);
		map.put("user_id", user_id);
		
		List<Home_memo> hList = null;
		int count = 0;
		if(user_id==null || user_id.equals("")) {
			
		}else {
			hList = hm.memo_select(map);
			count = hm.memo_count(map);
		}
		 
		ModelAndView mav = new ModelAndView("myhome/memo/memo_main");
		mav.addObject("list", hList);
		mav.addObject("count", count);
		mav.addObject("listNum" , num);
		return mav;
	}
	
	/**
	 * 메모 가져오기
	 */
	@RequestMapping(value=url + "/select.zpr" , params={"!nouse"})
	public ModelAndView memo_select(HttpServletRequest req , String memo , String title , String memo_seq , String cur , String num ) {
		HomeMapper hm = sqlSession.getMapper(HomeMapper.class);
		
		SessionManager sess = SessionManager.getInstance(req);
		
		String user_id = sess.getLoginEmail();
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("memo_seq", memo_seq);
		map.put("memo", memo);
		map.put("title", title);
		num = StringUtil.nvl(num, "5");
		map.put("num", num);
		cur = StringUtil.nvl(cur, "0");
		map.put("cur", cur);
		map.put("user_id", user_id);
		
		List<Home_memo> hList = hm.memo_select(map);
		
		ModelAndView mav = new ModelAndView("common/xmldata");
		mav.addObject("list", hList);
		
		return mav;
	}

	/**
	 * 메모 총 갯수 가져오기
	 */
	@RequestMapping(value=url + "/count.zpr" , params={"!nouse"})
	public ModelAndView memo_count(String user_id) {
		HomeMapper hm = sqlSession.getMapper(HomeMapper.class);
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", user_id);
		int count = 0;
		if(user_id == null || user_id.equals("") ) ; 
		else count = hm.memo_count(map);
		
		ModelAndView mav = new ModelAndView("common/result");
		mav.addObject("result", count);
		return mav;
	}
	
	/**
	 * 메모 저장
	 */
	@RequestMapping(value=url + "/insert.zpr" , params={"!nouse"})
	public ModelAndView memo_insert( HttpServletRequest req , String memo , String title ) {
		HomeMapper hm = sqlSession.getMapper(HomeMapper.class);
		
		SessionManager sess = SessionManager.getInstance(req);
		
		String user_id = sess.getLoginEmail();
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("memo", memo);
		map.put("title", title);
		map.put("user_id", user_id);
		int retCnt = hm.memo_insert(map);
		
		ModelAndView mav = new ModelAndView("common/result");
		mav.addObject("result", retCnt);
		return mav;
	}

	/**
	 * 메모 보기
	 */
	@RequestMapping(value=url + "/viewer.zpr" , params={"!nouse"})
	public ModelAndView memo_viewer( HttpServletRequest req , String memo_seq ) {
		HomeMapper hm = sqlSession.getMapper(HomeMapper.class);
		
		SessionManager sess = SessionManager.getInstance(req);
		
		String user_id = sess.getLoginEmail();
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("memo_seq", memo_seq);
		map.put("cur", 0);
		map.put("num", 1);
		map.put("user_id", user_id);
		
		Home_memo memoObj = null;
		if(memo_seq == null || memo_seq.equals("")) {
		}
		else {
			List<Home_memo> memoList = hm.memo_select(map);
			if(memoList != null && memoList.size()>=1) memoObj = memoList.get(0);
		}
		ModelAndView mav = new ModelAndView("myhome/memo/memo_viewer");
		mav.addObject("memoObj", memoObj);
		return mav;
	}
	
	/**
	 * 메모 삭제
	 */
	@RequestMapping(value=url + "/delete.zpr" , params={"!nouse"})
	public ModelAndView memo_delete( String memo_seqs ) {
		HomeMapper hm = sqlSession.getMapper(HomeMapper.class);
		
		String[] memos = memo_seqs.split("#");
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		int delCnt = 0;
		
		for( String memo_seq : memos ) {
			if(memo_seq == null || memo_seq.equals("")) continue;
			map.put("memo_seq", memo_seq);
			delCnt += hm.memo_delete(map);
		}
		
		ModelAndView mav = new ModelAndView("common/result");
		mav.addObject("result", delCnt);
	
		return mav;
	}

	/**
	 * 메모 수정
	 */
	@RequestMapping(value=url + "/update.zpr" , params={"!nouse"})
	public ModelAndView memo_update( String memo_seq , String title , String memo ) {
		HomeMapper hm = sqlSession.getMapper(HomeMapper.class);
		
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("memo_seq" , memo_seq);
		map.put("title" , title);
		map.put("memo" , memo);
		
		int ret = hm.memo_update(map);
		
		
		ModelAndView mav = new ModelAndView("common/result");
		mav.addObject("result", ret);
		
		return mav;
	}
	
		
}